<?php

// +----------------------------------------------------------------------
// | 处理app token相关的业务
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

namespace app\api\service;

use app\common\lib\exception\ApiException;
use app\api\model\ThirdApp as ThirdAppModel; 
use app\common\lib\enum\ScopeEnum;


class AppToken extends Token
{
    /**
    * 获取token
    * @param string 
    * @param string 
    * @return array
    */
    public function get($ac,$se)
    {
        $app = ThirdAppModel::check($ac,$se);

        if(!$app){
            throw new ApiException("授权失败", 403,10004);            
        }else {
            $scope = $app->scope;
            $uid = $app->id;
            $values = [
                'scope' => $scope,
                'uid' => $uid
            ];
            $token = $this->saveToCache($values);
            return $token;
        }
    }
    /**
     * 获取token
     */
    private function saveToCache($values){
        $token = self::generateToken();
        $expire_in = config('api.token_expire_in');
        $result = cache($token, json_encode($values), $expire_in);
        if(!$result){
            throw new ApiException('服务器缓存异常',500,10005);
        }
        return $token;
    }
   
}
